package com.yhl.vocation.util;
import com.alibaba.fastjson2.JSON;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import java.io.UnsupportedEncodingException;


public class WxUtil {

    public static RestTemplate restTemplate = new RestTemplate();

    public static String baseUrl = "https://api.weixin.qq.com/sns/jscode2session?appid=wxc69bf9004e1baee8&secret=8ac8a1af24bf8cba468d242aec552dc3&js_code=%s&grant_type=authorization_code";

    public static String getOpenIdByCode(String code) throws UnsupportedEncodingException {
        String accessUrl = String.format(baseUrl, code);
        ResponseEntity<String> forEntity = restTemplate.getForEntity(accessUrl, String.class);
        return (String) JSON.parseObject(forEntity.getBody()).get("openid");
    }
}
